package cn.itcast.controller.user;

import cn.itcast.base.BaseController;
import cn.itcast.domain.Dept;
import cn.itcast.domain.Role;
import cn.itcast.domain.User;
import cn.itcast.service.dept.DeptService;
import cn.itcast.service.role.RoleService;
import cn.itcast.service.user.UserService;
import cn.itcast.utils.UtilFuns;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Controller
@RequestMapping("/system/user")
public class UserController extends BaseController {

    @Autowired
    private UserService userService;
    @Autowired
    private DeptService deptService;
    @Autowired
    private RoleService roleService;
    @Autowired
    private AmqpTemplate amqpTemplate;



    @RequestMapping(name = "pageHelper插件分页查询用户",value = "/list")
    public String findAll(@RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "5") int pageSize){

        PageInfo pb = userService.findAll(pageNum, pageSize, company_id);
        request.setAttribute("pb",pb);
        return "system/user/user-list";

    }

    @RequestMapping(name = "到新建用户页面",value = "/toAdd")
    public String toAdd(){
        //查询所有部门
        List<Dept> list= deptService.findAllDept(company_id);
        request.setAttribute("deptList",list);
        return "system/user/user-add";
    }

    @RequestMapping(name = "用户保存/用户更新",value = "/edit")
    public String edit(User user){

        //指定当前用户是属于哪个公司的
        user.setCompanyId(company_id);
        user.setCompanyName(company_name);
        //要给页面的密码也做md5加密
        Md5Hash md5Password = new Md5Hash(user.getPassword(), user.getEmail(), 1);
        user.setPassword(md5Password.toString());
        if(UtilFuns.isEmpty(user.getId())){
            // 新增
            user.setId(UUID.randomUUID().toString());
            // 将注册的数据保存到数据库
            userService.save(user);
            // 给注册用户的邮箱发一封欢迎邮件
                        //任务：充当生产者望消息中间件中放入内容
            Map map = new HashMap();
            map.put("email",user.getEmail());
            map.put("emailMsg","欢迎成为saas大家庭的一员~");
            amqpTemplate.convertAndSend("saas.email",map);

        }else{
            // 更新
            userService.update(user);

        }

        return "redirect:/system/user/list.do";
    }



    @RequestMapping(name = "到修改用户页面回显数据",value = "/toUpdate")
    public String toUpdate(String id){
        User user=userService.findById(id);
        request.setAttribute("user",user);

        //查询所有部门
        List<Dept> list= deptService.findAllDept(company_id);
        request.setAttribute("deptList",list);
        return "system/user/user-update";
    }


    @RequestMapping(name = "用户数据删除",value = "/delete")
    public String delete(String id){

        userService.delete(id);

        return "redirect:/system/user/list.do";
    }


    @RequestMapping(name = "到用户选角色页面",value = "/roleList")
    public String roleList(String id){

        //1 查询当前传递id的用户
        User user = userService.findById(id);
        //2 查询所有的角色
        List<Role> roleList=roleService.findAllRole(company_id);
        //3 查询当前用户具备的角色
        List<Role> userRoleList=roleService.findUserRole(id);
        //4 将当前用户具备的角色进行字符串拼接
        String str="";
        for (Role role : userRoleList) {
            str+=role.getId()+",";        //"1,2,3,"
        }
        //5 带着数据到页面展示
        request.setAttribute("user",user); //用户
        request.setAttribute("roleList",roleList); //所有的角色
        request.setAttribute("userRoleStr",str); // 用户角色的id拼接
        return "/system/user/user-role";
    }



    @RequestMapping(name = "用户设置角色",value = "/changeRole")
    public String changeRole(String userid,String[] roleIds){

        userService.changeRole(userid,roleIds);

        return "redirect:/system/user/list.do";
    }

}
